﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using KandaAlpha.UI.Mvc.Infrastructure.Mvc;
using KandaAlpha.Application.Interfaces;
using KandaAlpha.Domain.Model.Entities;
using KandaAlpha.Domain.Model.ValueObjects;
using KandaAlpha.UI.Mvc.Models; 

namespace KandaAlpha.UI.Mvc.Controllers
{
    public class ClassifierController : ControllerAppBase
    {
        public ClassifierController(IApplicationService appService)
            : base(appService)
        {
            Model = CreateViewData<ClassifierViewData>(); 
        }


        protected ClassifierViewData Model
        {
            get
            {
                return ViewData.Model as ClassifierViewData;
            }
            set
            {
                ViewData.Model = value;
            }
        }


        //
        // GET: /Classifier/

        public ActionResult Index()
        {
            Model.Title = "Classifier List";
            Model.Classifiers =  ClassifierService.GetAll();
            return View();
        }

        //
        // GET: /Classifier/Details/5

        public ActionResult Details(int id)
        {
            Model.Classifier = ClassifierService.Load(id);
            if (Model.Classifier == null)
                return View("NotFound");
            Model.Title = Model.Classifier.ClassifierName;
            return View();
        }

        //
        // GET: /Classifier/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Classifier/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            var classifier = new Classifier();
            using (var uow = UnitOfWorkFactory.Create())
            {
                TryUpdateModel(classifier); 
                ClassifierService.Add(classifier);
                uow.Commit();
            }

            return RedirectToAction("Details", new { id = classifier.ID });

        }

        //
        // GET: /Classifier/Edit/5
 
        public ActionResult Edit(int id)
        {
            Model.Classifier = ClassifierService.Load(id);
            if (Model.Classifier == null)
                return View("NotFound");
            Model.Title = "Edit " + Model.Classifier.ClassifierName;
            return View();
        }

        //
        // POST: /Classifier/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {

            var classifier = ClassifierService.Load(id);
            if (classifier == null)
                return View("NotFound");

            using (var uow = UnitOfWorkFactory.Create())
            {
                TryUpdateModel(classifier); 
                ClassifierService.Update(classifier);
                uow.Commit();
            }

            return RedirectToAction("Details", new { id = classifier.ID });

        }
    }
}
